﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;

namespace eBusiness.App_DAL
{
    public static class Utils
    {
        public static Expression<Func<T, bool>> OrElse<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
        {
            //var param = Expression.Parameter(typeof(T), "travel");
            ParameterExpression param = left.Parameters[0];
            if (ReferenceEquals(param.Type, right.Parameters[0].Type))
            {
                // simple version
                //return Expression.Lambda<Func<T, bool>>(
                //    Expression.And(left.Body, right.Body), param);
                return Expression.Lambda<Func<T, bool>>(Expression.And(left.Body,Expression.Invoke(right, param)),param);
            }
            //var body = Expression.OrElse(
            //        left.Body,
            //        right.Body
            //    );
            //var lambda = Expression.Lambda<Func<T, bool>>(body, param);
            return null;
        }

        public static string GetLeft(this string obj, int length, bool isFullWord)
        {
            if (String.IsNullOrEmpty(obj))
            {
                return "";
            }
            if (obj.Length <= length)
            {
                return obj;
            }
            if (isFullWord)
            {
                //var ch = obj[length - 1];

                //else
                //{
                int indexToBreak = 0;
                for (int i = length - 1; i < obj.Length; i++)
                {
                    if ((obj[i] == ' ') || (obj[i] == '\r') || (obj[i] == '\n') || (obj[i] == '\t'))
                    {
                        indexToBreak = i;
                        break;
                    }
                }
                return obj.Substring(0, indexToBreak + 1) + "...";
                //}
            }
            return obj.Substring(0, length) + "...";
        }
    }
}